app.controller("DailyController",['$scope','$http','myPage', function($scope, $http, myPage){
    // 超级管理员权限
    $http({
        method: 'GET',
        url: '/OA/staff/roles',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    }).success(function (response) {
        $scope.roles=[];
        $scope.roles=response.data;
        console.log($scope.roles)
        function permissions(){
            var rs=false;
            for (var i = 0; i < $scope.roles.length; i++) {
                if ($scope.roles[i] == 7) {
                    rs = true;
                }else{
                    rs= false;
                }

            }
            return rs
        }
        $scope.admin=permissions();
        console.log($scope.admin)
    })
    $scope.myPage={
        currentPage:1,//访问第几页数据，从1开始
        totalItems:0,//数据库中总共有多少条数据
        itemsPerPage: 6,//默认每页展示多少条数据，可更改
        pagesLength: 15,
        perPageOptions: [10, 20, 30, 40, 50,60]//可选择的每页展示多少条数据
    };
    //监测当页码。总数据，每页展示数据个数变化时，重新加载数据
    $scope.$watch(function ()
    {return $scope.myPage.itemsPerPage+' '+$scope.myPage.currentPage+' '+$scope.myPage.totalItems;},getList) ;
    function getList(){
        //获取列表需要时，将页码重置为1
        $scope.myPage.currentPage=myPage.pageNub;
        //传给服务器时，页码从0开始
        // 定义查询按钮
        var listJson={};
        function changeJson(){
            var studentCard=$("#studentcard").val();
            var studentName=$("#studentname").val();
            var staffId=$("#responsible").find("option:selected").val();
            var campus_id=$("#schoolcampus").find("option:selected").val();
            return {pageNumber:myPage.pageNub,staff_id:staffId,name:studentName,sn:studentCard,campus_id:campus_id}
        }
        listJson=changeJson();
        $scope.find=function () {
            listJson=changeJson();
            console.log(listJson)
            Json()
        }
        Json();
        function  Json() {
            $http({
                method: 'POST',
                url: '/OA/student/list',
                data: $.param(listJson),
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
            }).success(function (response) {
                console.log(response.data)
                $scope.datas=response.data.list;
                $scope.pageSize=response.data.pageSize;
                $scope.pageNumber=response.data.pageNumber;
                $scope.myPage.totalItems=response.data.totalRow;//当获取总数据后，修改默认值;
                // 定义一个空对象 , 用于更新和保存数据时临时存储
                $scope.prod = {};
                $scope.arrs=$scope.datas;
                // 定义一个单击删除按钮时触发的事件，用于删除选中行
                $scope.delete = function($index){
                    if ($index>=0) {
                        if(confirm("是否删除"+$scope.arrs[$index].name+"的信息") ){
                            //将需要删除的卡号传给后台  2017/7/10
                            $http({
                                method: 'POST',
                                url: '/OA/student/delete',
                                data: $.param({id:$scope.arrs[$index].id}),
                                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
                            }).success(function (result) {
                                if (result.ok==true) {
                                    $scope.arrs.splice($index,1);
                                }
                            }).error(function(result){
                                alert("删除失败")
                            })
                        }
                    };
                }
                // 查看员工信息;
                $scope.ToView=function($index){
                    $('#check').modal('show');
                    $http({
                        method:"POST",
                        url:"/OA/student/query",
                        data:$.param({id:$scope.arrs[$index].id}),
                        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
                    }).success(function (response){
                        console.log(response)
                        if(response.ok==true){
                            console.log(response.data)
                            $scope.findContent=response.data;
                        }
                    })
                };
                // 定义一个空对象 , 用于更新和保存数据时临时存储
                $scope.prod = {};
                //定义一个单击修改按钮时触发的事件，用于单击后弹出模块窗口用于修改报课单数据
                $scope.updata = function ($index) {
                    //显示bootstrap中的模块窗口
                    console.log($scope.arrs[$index])
                    $('#revise').modal('show');
                    //将选中行的数据绑定到临时对象prod中，在下面的模态窗口中展示出来
                    $scope.prod.id = $scope.arrs[$index].id;
                    $scope.prod.sn = $scope.arrs[$index].sn;
                    $scope.prod.name = $scope.arrs[$index].name;
                    $scope.prod.sex = $scope.arrs[$index].sex;
                    $scope.prod.school = $scope.arrs[$index].school;
                    $scope.prod.grade_id = $scope.arrs[$index].grade_id;
                    $scope.prod.grade_name = $scope.arrs[$index].grade_name;
                    $scope.prod.direction=$scope.arrs[$index].direction;
                    $scope.prod.wechat = $scope.arrs[$index].wechat;
                    $scope.prod.mobile1 = $scope.arrs[$index].mobile1;
                    $scope.prod.mobile2 = $scope.arrs[$index].mobile2;
                    $scope.prod.mobile3 = $scope.arrs[$index].mobile3;
                    $scope.prod.staff_type = $scope.arrs[$index].staff_type;
                    $scope.prod.staff_id = $scope.arrs[$index].staff_id;
                    $scope.prod.staff_consulting_id = $scope.arrs[$index].staff_consulting_id;
                    $scope.prod.staff_consulting_name = $scope.arrs[$index].staff_consulting_name;
                    $scope.prod.staff_education_name = $scope.arrs[$index].staff_education_name;
                    $scope.prod.staff_education_id = $scope.arrs[$index].staff_education_id;
                    $("#updatasex").find("option[value="+$scope.prod.sex+"]").prop("selected","selected");
                    $("#updatagrade").find("option[value="+$scope.prod.grade_id+"]").prop("selected","selected");
                    $("#updataconsulting").find("option[value="+$scope.prod.staff_consulting_id+"]").prop("selected","selected");
                    $("#updataadministration").find("option[value="+$scope.prod.staff_education_id+"]").prop("selected","selected");
                    // 修改后保存
                    $scope.save = function () {
                        var id=$scope.prod.id;
                        var sn=$("#updatasn").val();
                        var name=$("#updataname").val();
                        var sex=$("#updatasex").find("option:selected").val();
                        var school=$("#updataschool").val();
                        var grade_id=$("#updatagrade").find("option:selected").val();
                        var grade_name=$("#updatagrade").find("option:selected").text();
                        var mobile1=$("#updatamobile1").val();
                        var mobile2=$("#updatamobile2").val();
                        var mobile3=$("#updatamobile3").val();
                        var wechat=$("#updatawechat").val();
                        function staff_type() {
                            if($("#updataconsulting").find("option:selected").val()==""&&$("#updataadministration").find("option:selected").val()==""){
                                return 0
                            }else if($("#updataconsulting").find("option:selected").val()==""){
                                return 2
                            }else if($("#updataadministration").find("option:selected").val()==""){
                                return 1
                            }
                        }
                        var staff_type=staff_type();
                        function staff_id() {
                            if($("#updataconsulting").find("option:selected").val()==""&&$("#updataadministration").find("option:selected").val()==""){
                                alert("请选择一位咨询或者教务");
                                return false
                            }else if($("#updataconsulting").find("option:selected").val()==""){
                                return $("#updataadministration").find("option:selected").val();
                            }else if($("#updataadministration").find("option:selected").val()==""){
                                return $("#updataconsulting").find("option:selected").val()
                            }
                        }
                        var staff_id=staff_id();
                        if(staff_id==false)
                            return;
                        var staff_consulting_id=$("#updataconsulting").find("option:selected").val();
                        var staff_consulting_name=$("#updataconsulting").find("option:selected").text();
                        var staff_education_id=$("#updataadministration").find("option:selected").val();
                        var staff_education_name=$("#updataadministration").find("option:selected").text();
                        if(staff_consulting_id==""&&staff_education_id==""){
                            alert("请选择一位咨询或者教务!")

                        }
                        $scope.updataJson={id:id,sn:sn,name:name,sex:sex,school:school,grade_id:grade_id,grade_name:grade_name,wechat:wechat,mobile1:mobile1,mobile2:mobile2,mobile3:mobile3,staff_type:staff_type,staff_id:staff_id,staff_consulting_id:staff_consulting_id,staff_education_id:staff_education_id,staff_consulting_name:staff_consulting_name,staff_education_name:staff_education_name};
                        console.log($scope.updataJson)
                        $http({
                            method:"POST",
                            url:"/OA/student/update",
                            data:$.param($scope.updataJson),
                            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
                        }).success(function (response) {
                            $('#revise').modal('hide');
                            setTimeout(function () {
                                window.location.reload()
                            },600)
                        })
                    }
                };
            })
        }
    }
    // 补全信息
    var ajax = new XMLHttpRequest();
    ajax.open("GET", "/OA/fromschool/list", true);
    ajax.onload = function() {
        var list = JSON.parse(ajax.responseText).map(function(i) { return i.name; });
        new Awesomplete(document.querySelector("#schoolname"),{ list: list });
    };
    ajax.send();
    //添加学员API
    // time: 2017/9/27
    $scope.addstudent=function(){
        var sn=$scope.studentsn;
        var name=$scope.studentname;
        var sex=$("#studentsex").find("option:selected").val();
        var school=$scope.schoolname;
        var grade_id=$("#grade").find("option:selected").val();
        var grade_name=$("#grade").find("option:selected").text();
        var wechat=$scope.wechat;
        var mobile1=$scope.mobile1;
        var mobile2=$scope.mobile2;
        var mobile3=$scope.mobile3;
        var direction=$("#type_class").val();
        function staff_type() {
            if($("#consulting").find("option:selected").val()==""&&$("#administration").find("option:selected").val()==""){
                return 0
            }else if($("#consulting").find("option:selected").val()==""){
                return 2
            }else if($("#administration").find("option:selected").val()==""){
                return 1
            }
        }
        var staff_type=staff_type();
        function staff_id() {
            if($("#consulting").find("option:selected").val()==""&&$("#administration").find("option:selected").val()==""){
                alert("请选择一位咨询或者教务");
                return false
            }else if($("#consulting").find("option:selected").val()==""){
                return $("#administration").find("option:selected").val();
            }else if($("#administration").find("option:selected").val()==""){
                return $("#consulting").find("option:selected").val()
            }
        }
        var staff_id=staff_id();
            if(staff_id==false)
                return;
        var staff_consulting_id=$("#consulting").find("option:selected").val();
        var staff_consulting_name=$("#consulting").find("option:selected").text();
        var staff_education_id=$("#administration").find("option:selected").val();
        var staff_education_name=$("#administration").find("option:selected").text();

        $scope.addstudent={sn:sn,name:name,sex:sex,school:school,grade_id:grade_id,grade_name:grade_name,wechat:wechat,mobile1:mobile1,mobile2:mobile2,mobile3:mobile3,staff_type:staff_type,staff_id:staff_id,staff_consulting_id:staff_consulting_id,staff_education_id:staff_education_id,staff_consulting_name:staff_consulting_name,staff_education_name:staff_education_name,direction:direction}
        console.log($scope.addstudent);
        $http({
            method: 'POST',
            url: '/OA/student/create',
            data: $.param($scope.addstudent),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function (response){
            if (response.ok== true) {
                alert(response.ok)
                $("#studentadd").modal('hide')
                setTimeout(function () {
                    window.location.reload()
                },600)
            };
        })
    }
    $scope.change = function(selectedLimit){
        $scope.page.limit = selectedLimit.limit;
    }
    $scope.pageChanged = function(){
        $scope.page.limit = $scope.selectedLimit.limit;
    }
    $scope.setPage = function (go) {
        $scope.length = Math.ceil($scope.total/$scope.selectedLimit.limit);
        console.log($scope.length);
        if(go > $scope.length){
            $scope.page.pageNo =  $scope.length;
            console.log($scope.length);
        }else{
            $scope.page.pageNo=go;
        }
    };
}]);


